﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTGroup_ZheJiang.IDAL;
using System.Collections.Concurrent;

namespace DTGroup_ZheJiang.OracleDataAccess
{
    public class DaoFactory : IDaoFactory
    {
        /// <summary>
        /// ConcurrentDictionary&lt;TKey,TValue&gt;线程安全的字典存储器
        /// </summary>
        private ConcurrentDictionary<Type, object> dicMap = new ConcurrentDictionary<Type, object>();

        public T Create<T>()
        {
            Type type = typeof(T);
            object value = null;

            if (dicMap.TryGetValue(type, out value))
            {
                return (T)value;
            }
            else
            {
                var classType = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(s => type.IsAssignableFrom(s)
                    && !s.IsInterface).FirstOrDefault();
                var instance = Activator.CreateInstance(classType);

                dicMap[type] = instance;

                return (T)instance;
            }
        }
    }
}
